package com.tbit.main.service.impl;

import com.tbit.main.dao.core.BatteryGroupBoundDao;
import com.tbit.main.dao.core.BatteryGroupDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Battery;
import com.tbit.main.pojo.BatteryGroup;
import com.tbit.main.pojo.BatteryGroupBound;
import com.tbit.main.service.*;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2022-05-03 15:20
 * 电池分组
 */
@Service("batteryGroupBoundService")
public class BatteryGroupBoundServiceImpl implements BatteryGroupBoundService {

    @Autowired
    private BatteryGroupBoundDao batteryGroupBoundDao;
    @Autowired
    private BatteryGroupDao batteryGroupDao;
    @Autowired
    private DataService dataService;
    @Autowired
    private BatteryLogService batteryLogService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private BatteryGroupService batteryGroupService;

    @Override
    public List<Battery> getBatteryNO(String batteryNO) {

        List<Battery> batteries = batteryGroupBoundDao.getBatteryNO(batteryNO);

        for (Battery battery : batteries) {

            battery.setAddAccountUserName(dataService.getAccountUser(battery.getAddAccountUserId()) == null ? null : dataService.getAccountUser(battery.getAddAccountUserId()).getName());
            battery.setAccountUserName(dataService.getAccountUser(battery.getAccountUserId()) == null ? null : dataService.getAccountUser(battery.getAccountUserId()).getName());
        }

        return batteries;
    }

    @Override
    public List<BatteryGroupBound> getId(Integer id) {
        return batteryGroupBoundDao.getId(id);
    }

    @Override
    public void del(Integer id) {
        batteryGroupBoundDao.del(id);
    }

    @Override
    public void addBatteryNOs(Integer id, List<String> batteryNOs) {
        List<BatteryGroupBound> batteryGroupBounds = new LinkedList<>();

        for (String batteryNO : batteryNOs) {
            BatteryGroupBound batteryGroupBound = new BatteryGroupBound();
            batteryGroupBound.setId(id);
            batteryGroupBound.setBatteryNO(batteryNO);
            batteryGroupBound.setBatteryId(dataService.getBatteryId(batteryNO));
            batteryGroupBounds.add(batteryGroupBound);
        }
        batteryGroupBoundDao.addBatteryNOs(batteryGroupBounds);
    }

    @Override
    public void addBatteryNO(Integer id, String batteryNO, Integer accountUserId) {
        BatteryGroup batteryGroup = batteryGroupDao.getId(id);
        if (batteryGroup != null) {
            BatteryGroupBound batteryGroupBound = batteryGroupBoundDao.getNO(batteryNO);
            if (batteryGroupBound != null) {
                throw new BaseException("Group.isBattery.notNull");
            } else {
                batteryGroupBoundDao.addBatteryNO(id, batteryNO, dataService.getBatteryId(batteryNO));
            }
        } else {
            throw new BaseException("Group.is.null");
        }
    }

    @Override
    public void delBatteryNO(String batteryNO) {
        BatteryGroupBound batteryGroupBound = batteryGroupBoundDao.getNO(batteryNO);
        if (batteryGroupBound != null) {
            batteryGroupBoundDao.delBatteryNO(batteryNO);
        }
    }

    @Override
    public List<BatteryGroup> getGroupAccountId(Integer accountId) {

        List<BatteryGroup> batteryGroups = batteryGroupService.getAccountId(accountId);

        List<BatteryGroupBound> batteryGroupBounds = batteryGroupBoundDao.getGroupAccountId(accountId);

        List<Integer> accountUserIds = batteryGroups.stream().map(BatteryGroup::getAccountUserId).collect(Collectors.toList());
        if (accountUserIds != null && accountUserIds.size() > 0) {
            List<AccountUser> accountUsers = accountUserService.getByIds(accountUserIds);

            Map<Integer, AccountUser> accountUserMap = new HashMap<>();
            for (AccountUser accountUser : accountUsers) {
                accountUserMap.put(accountUser.getAccountUserId(), accountUser);
            }

            Map<Integer, Integer> batteryGroupBoundMap = new HashMap<>();
            for (BatteryGroupBound batteryGroupBound : batteryGroupBounds) {
                batteryGroupBoundMap.put(batteryGroupBound.getId(), batteryGroupBound.getIdCount());
            }

            for (BatteryGroup batteryGroup : batteryGroups) {
                batteryGroup.setGroupId(batteryGroup.getId());
                batteryGroup.setAccountUserName(accountUserMap.get(batteryGroup.getAccountUserId()).getName());
                batteryGroup.setIdCount(batteryGroupBoundMap.get(batteryGroup.getId()));
            }
        }

        return batteryGroups;
    }

    @Override
    public BatteryGroupBound getNO(String batteryNO) {
        return batteryGroupBoundDao.getNO(batteryNO);
    }

    @Override
    public List<BatteryGroupBound> getByBatteryNOs(List<String> batteryNOs) {
        return batteryGroupBoundDao.getByBatteryNOs(StringUtil.getArrayListString(batteryNOs));
    }


    @Override
    public List<BatteryGroupBound> getNOs(String batteryNO) {
        return batteryGroupBoundDao.getNOs(batteryNO);
    }

    @Override
    public List<Battery> getBatteryById(Integer id) {

        List<Battery> batteries = batteryGroupBoundDao.getBatteryById(id);

        for (Battery battery : batteries) {
            battery.setAddAccountUserName(dataService.getAccountUser(battery.getAddAccountUserId()) == null ? null : dataService.getAccountUser(battery.getAddAccountUserId()).getName());
            battery.setAccountUserName(dataService.getAccountUser(battery.getAccountUserId()) == null ? null : dataService.getAccountUser(battery.getAccountUserId()).getName());
        }

        return batteries;
    }
}
